package com.pinusmusic.web.serlvet;

import com.pinusmusic.domain.*;
import com.pinusmusic.service.FavoriteService;
import com.pinusmusic.service.impl.FavoriteServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/favorite/*")
public class FavoriteServlet extends BaseServlet{

    FavoriteService favoriteService=new FavoriteServiceImpl();

    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String mid=req.getParameter("mid");
        List<User> user = (List<User>) req.getSession().getAttribute("user");
        if (user==null){
            writeValue(resp,"用户未登录！");
            return;
        }
        System.out.println("user:"+user.toString());
        User u = user.get(0);
        Favorite favorite=new Favorite();
        favorite.setMid(Integer.parseInt(mid));
        favorite.setUid(u.getUid());
        System.out.println("写入的favorite对象是"+favorite);
        ResultInfo info=favoriteService.favoriteAdd(favorite);
        this.writeValue(resp,info);
        System.out.println("收藏添加状态"+info.isFlag());
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fid=req.getParameter("fid");
        ResultInfo info=favoriteService.favoriteDelete(Integer.parseInt(fid));
        this.writeValue(resp,info);
        System.out.println("收藏添加状态"+info.isFlag());
    }

    public void findByUid(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException{
        List<User> user = (List<User>) req.getSession().getAttribute("user");
        List<MusicInfo> music=favoriteService.favoriteFindByUid(user.get(0).getUid());
        this.writeValue(resp,music);
//        System.out.println("评论查询状态"+info.isFlag());
    }
    public void deletefav(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String _mid = req.getParameter("mid");
        int mid = Integer.parseInt(_mid);
        favoriteService.deleteFavByMid(mid);
//        as.deleteFavByUid(uid);
//        as.deleteComByUid(uid);
    }
    public void deletefavs(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String mids = req.getParameter("mids");
        favoriteService.deleteFavByMid(Integer.parseInt(mids));
//        as.deleteFavByUid(Integer.parseInt(uids));
//        as.deleteComByUid(Integer.parseInt(uids));
    }
}
